import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router'
import permission from './permission'

export const defaultRoutes: Readonly<RouteRecordRaw[]> = [
  {
    path: '/',
    redirect: '/home',
    meta: {
      layout: true,
    },
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('../views/home/index.vue'),
    meta: {
      layout: true,
    },
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login/index.vue'),
    meta: {
      layout: false,
    },
  },
]
export const permissionRoutes: Readonly<RouteRecordRaw[]> = [
  {
    path: '/oms',
    name: 'oms',
    redirect: '/oms/apply',
    children: [
      {
        path: 'apply',
        name: 'returnApply',
        component: () => import('@/views/oms/apply/index.vue'),
        meta: {
          layout: true,
        },
      },
      {
        path: 'applyDetail',
        name: 'applyDetail',
        component: () => import('@/views/oms/apply/detail.vue'),
      },
      {
        path: 'applyReason',
        name: 'returnReason',
        component: () => import('@/views/oms/apply/reason.vue'),
        meta: {
          layout: true,
        },
      },
      {
        path: 'order',
        name: 'order',
        component: () => import('@/views/oms/order/index.vue'),
        meta: {
          layout: true,
        },
      },
      {
        path: 'order/detail',
        name: 'orderDetail',
        component: () => import('@/views/oms/order/detail.vue'),
      },
      {
        path: 'order/deliver',
        name: 'orderDeliver',
        props: (route) => ({ list: JSON.parse((route.query?.list as string) || '[]') }),
        component: () => import('@/views/oms/order/deliver.vue'),
      },
      {
        path: 'order/setting',
        name: 'orderSetting',
        component: () => import('@/views/oms/order/setting.vue'),
        meta: {
          layout: true,
        },
      },
    ],
  },
  {
    path: '/pms',
    name: 'pms',
    redirect: '/pms/brand',
    children: [
      {
        path: 'brand',
        name: 'pmsBrand',
        component: () => import('@/views/pms/brand/index.vue'),
        meta: {
          layout: true,
        },
      },
      {
        path: 'brand/upsert',
        name: 'brandUpsert',
        component: () => import('@/views/pms/brand/upsert.vue'),
        meta: {
          layout: true,
        },
      },
      {
        path: 'product',
        name: 'product',
        component: () => import('@/views/pms/product/index.vue'),
        meta: {
          layout: true,
        },
      },
      {
        path: 'productUpsert',
        name: 'productUpsert',
        component: () => import('@/views/pms/product/upsert.vue'),
      },
      {
        path: 'productType',
        name: 'productType',
        component: () => import('@/views/pms/productAttr/productType.vue'),
        meta: {
          layout: true,
        },
      },
      {
        path: 'productAttr',
        name: 'productAttr',
        component: () => import('@/views/pms/productAttr/index.vue'),
        meta: {
          layout: true,
        },
      },
      {
        path: 'productAttrUpsert',
        name: 'productAttrUpsert',
        component: () => import('@/views/pms/productAttr/upsert.vue'),
        meta: {
          layout: true,
        },
      },
      {
        path: 'productCate',
        name: 'productCate',
        component: () => import('@/views/pms/productCate/index.vue'),
        meta: {
          layout: true,
        },
      },
      {
        path: 'productCate/upsert',
        name: 'upsert',
        component: () => import('@/views/pms/productCate/upsert.vue'),
        meta: {
          layout: true,
        },
      },
    ],
  },
  {
    path: '/sms',
    name: 'sms',
    redirect: '/sms/advertise',
    children: [
      {
        path: 'advertise',
        name: 'advertise',
        component: () => import('@/views/sms/advertise/index.vue'),
        meta: {
          layout: true,
        },
      },
      {
        path: 'advertiseUpsert',
        name: 'advertiseUpsert',
        component: () => import('@/views/sms/advertise/upsert.vue'),
      },
      {
        path: 'brand',
        name: 'brand',
        component: () => import('@/views/sms/brand/index.vue'),
        meta: {
          layout: true,
        },
      },
      {
        path: 'coupon',
        name: 'coupon',
        component: () => import('@/views/sms/coupon/index.vue'),
        meta: {
          layout: true,
        },
      },
      {
        path: 'couponUpsert',
        name: 'couponUpsert',
        component: () => import('@/views/sms/coupon/upsert.vue'),
      },
      {
        path: 'couponDetail',
        name: 'couponDetail',
        component: () => import('@/views/sms/coupon/detail.vue'),
      },
      {
        path: 'flash',
        name: 'flash',
        component: () => import('@/views/sms/flash/index.vue'),
        meta: {
          layout: true,
        },
      },
      {
        path: 'flash/session',
        name: 'flashSession',
        component: () => import('@/views/sms/flash/session.vue'),
      },
      {
        path: 'flash/selectSession',
        name: 'flashSelectSession',
        component: () => import('@/views/sms/flash/selectSession.vue'),
      },
      {
        path: 'flash/productRelation',
        name: 'flashProductRelation',
        component: () => import('@/views/sms/flash/productRelation.vue'),
      },
      {
        path: 'hot',
        name: 'hot',
        component: () => import('@/views/sms/hot/index.vue'),
        meta: {
          layout: true,
        },
      },
      {
        path: 'new',
        name: 'new',
        component: () => import('@/views/sms/new/index.vue'),
        meta: {
          layout: true,
        },
      },
      {
        path: 'subject',
        name: 'subject',
        component: () => import('@/views/sms/subject/index.vue'),
        meta: {
          layout: true,
        },
      },
    ],
  },
  {
    path: '/ums',
    name: 'ums',
    redirect: '/ums/admin',
    children: [
      {
        path: 'admin',
        name: 'admin',
        component: () => import('@/views/ums/admin/index.vue'),
        meta: {
          layout: true,
        },
      },
      {
        path: 'menu',
        name: 'menu',
        component: () => import('@/views/ums/menu/index.vue'),
        meta: {
          layout: true,
        },
      },
      {
        path: 'resource',
        name: 'resource',
        component: () => import('@/views/ums/resource/index.vue'),
        meta: {
          layout: true,
        },
      },
      {
        path: 'resource/category',
        name: 'resourceCategory',
        component: () => import('@/views/ums/resource/category.vue'),
      },
      {
        path: 'role',
        name: 'role',
        component: () => import('@/views/ums/role/index.vue'),
        meta: {
          layout: true,
        },
      },
      {
        path: 'allocMenu',
        name: 'allocMenu',
        component: () => import('@/views/ums/role/allocMenu.vue'),
      },
      {
        path: 'allocResource',
        name: 'allocResource',
        component: () => import('@/views/ums/role/allocResource.vue'),
      },
    ],
  },
  {
    path: '/tms',
    name: 'tms',
    redirect: '/tms/table/margeCell',
    children: [
      {
        path: 'table/margeCell',
        name: 'tableMargeCell',
        component: () => import('@/views/tms/table/margeCell.vue'),
      },
    ],
  },
  {
    path: '/tool',
    name: 'tool',
    redirect: '/tool/swagger',
    children: [
      {
        path: 'swagger',
        name: 'tableMargeCell',
        component: () => import('@/views/tool/swagger/index.vue'),
        meta: {
          layout: true,
        },
      },
      {
        path: 'gen',
        name: 'gen',
        component: () => import('@/views/tool/gen/index.vue'),
        meta: {
          layout: true,
        },
      },
    ],
  },
]
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [...defaultRoutes, ...permissionRoutes],
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  },
})

permission(router)

export default router
